using System;
using System.Collections;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Context;
using PHSRAG.Insight21.Policy;
using PHSRAG.Insight21.SearchContext;
using PHSRAG.Utility;
using Utils = PHSRAG.Insight21.InsightUtilities;

/*at098 - 01/18/2008
 * This page is a prototype for SQL reports
 *  */

namespace PHSRAG.Insight21.Humans.Reports
{
    public partial class sqlReportHost : BasePage
    {
        private string _pageUrl = string.Empty;

        /*This page was built for prototype and the coding style shouldnot be followed.
         To Do -
         -CleanUp
         -Create a common routine for creating parameter collection*/

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string srptName = Request["rpt"]; //name of the report to serve up
                    string format = Request["fmt"]; //format of the report - PDF or Excel
                    _pageUrl = Request["pageUrl"]; //format of the report - PDF or Excel

                    if (!string.IsNullOrEmpty(srptName))
                    {
                        Hashtable tbl = GetInputParameters(srptName);
                        SSRSRequest.FetchAndDisplayReport(format, srptName, tbl);
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("There was a problem processing your request. Please contact helpdesk. <br>" + ex.Message);
                }
            }
        }

        # region InputParameters

        private Hashtable GetInputParameters(string srptName)
        {
            string sDelimitedIds;
            /*Get the instance of the Protocol Manager. This instance will give all the Id's of protocols on the report - first parameter
             * Get the instance of the UserContext - this will give the UserId - second paaremeter
             * The Search Parameters have to be passed to the report - NOT BEING DONE for PROTOTYPE
             */
            int Count = 0;
            ICachePolicy cachePolicy = (ICachePolicy) Session[Names.CachePolicyKey];
            ProtocolManager oPM = ResultsCache.GetObject(cachePolicy, ProtocolManager.CacheKey) as ProtocolManager;
            if (oPM != null)
            {
                Count = oPM.Count;
            }

            Hashtable tbl = new Hashtable();

            switch (srptName)
            {
                case SSRSRequest.ReportNames.Animals.ActiveProtocols: //"animals_activeprotocols":
                case SSRSRequest.ReportNames.Animals.InActiveProtocols: //"animals_inactiveprotocols":
                case SSRSRequest.ReportNames.Animals.ExemptProtocols:
                case SSRSRequest.ReportNames.Animals.WorkInProgress:
                case SSRSRequest.ReportNames.Humans.ActiveProtocols: //"humans_activeprotocols":
                case SSRSRequest.ReportNames.Humans.InActiveProtocols: //"humans_inactiveprotocols":
                case SSRSRequest.ReportNames.Humans.ExemptProtocols: //"humans_exemptprotocols":
                case SSRSRequest.ReportNames.Humans.WorkInProgress: //"humans_workinprogress":
                {
                    sDelimitedIds = oPM.GetDelimitedIds(0, Count, 0);
                    tbl.Add("delimittedString", sDelimitedIds);
                }
                    break;
                    //case "inactive_protocols":
                    //    {
                    //        srptName = "humans_protocols";
                    //        tbl.Add("reporttitle", "Inactive Protocols");                        

                    //        sDelimitedIds = oPM.GetDelimitedIds(0, Count,0);
                    //        tbl.Add("delimittedString", sDelimitedIds);                        
                    //    }
                    //break;
                    //case "humans_exemptprotocols":
                    //case "humans_workinprogress":
                    //    {
                    //        sDelimitedIds = oPM.GetDelimitedIds(0, Count, 0);
                    //        tbl.Add("delimittedString", sDelimitedIds);                        
                    //    }
                    //    break;
                case SSRSRequest.ReportNames.Animals.PendingApplications:
                case SSRSRequest.ReportNames.Humans.PendingApplications: //"humans_pendingapplications":
                {
                    //tbl.Add("StartIndex", oPM.StartIndex.ToString());
                    //tbl.Add("EndIndex", oPM.EndIndex.ToString());
                    //tbl.Add(SSRSRequest.ReportParameterKeys.SessionId, Session.SessionID);

                    sDelimitedIds = oPM.GetDelimitedIds(0, Count, 0); //0- will fetch the delimited protocol Id's
                    tbl.Add("protocolIdDelimittedString", sDelimitedIds);

                    sDelimitedIds = oPM.GetDelimitedIds(0, Count, 1); //1- will fetch the delimited protocol Process Id's
                    tbl.Add("protocolProcessIdDelimittedString", sDelimitedIds);

                    sDelimitedIds = oPM.GetDelimitedIds(0, Count, 2); //2- will fetch the delimited Board Review Id's
                    tbl.Add("boardReviewIdDelimittedString", sDelimitedIds);
                }
                    break;
                case SSRSRequest.ReportNames.Humans.HumansManagement:
                    break;
            }


            UserContext userContext = (UserContext) Session[UserContext.CacheKey];
            tbl.Add(SSRSRequest.ReportParameterKeys.DomainUserId, userContext.DomainUserId.ToString()); //Domain User Id

            tbl.Add(SSRSRequest.ReportParameterKeys.UserName, userContext.LastName + "," + userContext.FirstName); //username

            tbl.Add(SSRSRequest.ReportParameterKeys.FilterCriteria, GetFilterCriteriaForReport(cachePolicy)); //filtercriteria

            tbl.Add(SSRSRequest.ReportParameterKeys.WebUrl, Settings.GetKeyValue("weburl", "http://insight2.partners.org")); //Domain User Id

            return tbl;
        }

        private string GetFilterCriteriaForReport(ICachePolicy cachePolicy)
        {
            CommonSearchContext searchContext = cachePolicy.GetCachedObject(CommonSearchContext.CacheKey) as CommonSearchContext;
            if (searchContext != null)
            {
                SearchCriteria searchCriteria = searchContext.GetSearchCriteria(_pageUrl);
                return searchCriteria.ToString(";");
            }
            return string.Empty;
        }

        #endregion
    }
}